package com.alkaid.anime.controller;

import com.alkaid.anime.service.ThumbService;
import com.alkaid.commons.controller.BaseController;
import com.alkaid.commons.entity.Thumb;
import com.alkaid.commons.utils.JsonResult;
import com.alkaid.commons.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Kylin
 */
@RestController
@RequestMapping("api/anime")
public class ThumbController extends BaseController {

    private final ThumbService thumbService;

    public ThumbController(ThumbService thumbService) {
        this.thumbService = thumbService;
    }

    @GetMapping("/thumb")
    public JsonResult<Boolean> getThumb(HttpServletRequest request, @Validated Thumb thumb) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        thumb.setUserId(userId);
        Boolean result = thumbService.getThumb(thumb);
        return new JsonResult<>(200, result, "success");
    }

    @PostMapping("/thumb")
    public JsonResult<Void> setThumb(HttpServletRequest request, @Validated Thumb thumb) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        thumb.setUserId(userId);
        thumbService.setThumb(thumb);
        return new JsonResult<>(200, null, "success");
    }
}
